﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;
using ExtAspNet;


namespace TCMSystem.admin
{
    public partial class dept_edit : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Module

        /// <summary>
        /// 当前页面所属的模块
        /// </summary>
        public override ModuleType Module
        {
            get
            {
                return ModuleType.CoreDept;
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            // 权限检查
            if (!CheckPowerEdit())
            {
                CheckPowerFailWithPage();
                return;
            }

            btnClose.OnClientClick = ActiveWindow.GetConfirmHidePostBackReference();

            int id = GetQueryIntValue("id");
            TblMstDept current = TblMstDept.FetchByID(id);
            if (current == null)
            {
                // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }

            tbxName.Text = current.Name;
            tbxSortIndex.Text = current.SortIndex.ToString();
            tbxRemark.Text = current.Remark;

            // 绑定下拉列表
            BindDDL(current);
        }

        private void BindDDL(TblMstDept current)
        {
            TblMstDeptCollection items = new Select().From<TblMstDept>()
                .OrderAsc(TblMstDept.SortIndexColumn.ColumnName)
                .ExecuteAsCollection<TblMstDeptCollection>();
            List<CustomDept> mys = CustomDeptHelper.GetMyDeptCollection(items);

            ResolveDDL<CustomDept>(mys, current.Id);

            // 绑定到下拉列表（启用模拟树功能和不可选择项功能）
            ddlParent.EnableSimulateTree = true;
            ddlParent.DataTextField = "Name";
            ddlParent.DataValueField = "Id";
            ddlParent.DataSimulateTreeLevelField = "TreeLevel";
            ddlParent.DataEnableSelectField = "Enabled";
            ddlParent.DataSource = mys;
            ddlParent.DataBind();

            // 选中当前节点的父节点
            ddlParent.SelectedValue = current.ParentDeptId.ToString();
        }



        #endregion

        #region Events

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            int id = GetQueryIntValue("id");
            TblMstDept current = TblMstDept.FetchByID(id);
            current.Name = tbxName.Text.Trim();
            current.SortIndex = Convert.ToInt32(tbxSortIndex.Text.Trim());
            current.Remark = tbxRemark.Text.Trim();
            current.ParentDeptId = Convert.ToInt32(ddlParent.SelectedValue);
            current.Save(User.Identity.Name);

            //ExtAspNet.Alert.Show("保存成功！", String.Empty, ExtAspNet.Alert.DefaultIcon, ExtAspNet.ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        #endregion

    }
}
